/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.DistrictDao;
import com.techshop.model.District;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class DistrictDaoImpl extends HibernateDaoSupport implements DistrictDao
{
    @Override
    public District getById(int districtId)
    {
        try
        {
            return (District) getSession().get(District.class, districtId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<District> getByProvinceId(int provinceId)
    {
        try
        {
            return getSession().createQuery("FROM District WHERE province.id=:provinceId").setInteger("provinceId", provinceId).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(District district)
    {
        try
        {
            if (getById(district.getId()) != null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().save(district);
                ValidatePhonePubSubClient.changeDistrict();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
    
    @Override
    public boolean update(District district)
    {
        try
        {
            if (getById(district.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(district));
                ValidatePhonePubSubClient.changeDistrict();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(District district)
    {
        try
        {
            if (getById(district.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(district));
                ValidatePhonePubSubClient.changeDistrict();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
    
    @Override
    public List<District> getAll()
    {
        try
        {
            return getSession().createQuery("FROM District").list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
}